/**
 * Vuex
 * npm i vuex-persistedstate
 */

/* Vue2支持 */
// #ifdef VUE2
import Vue from 'vue'
import Vuex from 'vuex';
Vue.use(Vuex);
console.log('is Vue2');
// #endif

/* Vue3支持 */
// #ifdef VUE3
import { createStore } from 'vuex';
console.log('is Vue3');
// #endif

import createPersistedState from "vuex-persistedstate";

/* Load other Modules */
import user from '@/stores/modules/user.js'
import tabbar from "@/stores/modules/tabbar.js";
/*import payment from '@/store/payment.js'
 */


const store = {
	state: {
		// App-Version
		version: '1.0.0',
	},
	mutations: {
		/**
		 * 添加store业务代码 fn(state, vewValue) state.xxx = vewValue
		 */
		setVersion(state, value) {
			state.version = value;
		}
	},
	getters: {
		/**
		 * 衍生state ===> extend
		 */
	},
	// import Modules 引入的store模块
	modules: {
		user,
		tabbar
	},
	plugins: [
		createPersistedState({
			key: 'appStores',
			paths: [
				// index.js 直接写入 state 属性变量
				// 模块化则写入 import 模块 'xxx','xxx' 调用 xxx.$store.state.xxx.*
				'user',
				'tabbar'
			],
			storage: {
				getItem: key => uni.getStorageSync(key),
				setItem: (key, value) => uni.setStorageSync(key, value),
				removeItem: key => uni.removeStorageSync(key)
			}
		})
	]
};

// #ifdef VUE3
export default createStore(store); // Vue3导出
// #endif

// #ifdef VUE2
export default new Vuex.Store(store); // Vue2导出
// #endif